package luanvan.overlay;

import java.util.ArrayList;

import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	private Drawable marker = null;
	private OverlayItem inDrag = null;
	private ImageView dragImage = null;
	private int xDragImageOffset = 0;
	private int yDragImageOffset = 0;
	private int xDragTouchOffset = 0;
	private int yDragTouchOffset = 0;
	private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

	public MyItemizedOverlay(Drawable pDefaultMarker,
			ResourceProxy pResourceProxy, ImageView img) {
		super(pDefaultMarker, pResourceProxy);
		marker = pDefaultMarker;
		dragImage = img;
		xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
		yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();
		// TODO Auto-generated constructor stub
	}

	public void addItem(GeoPoint p, String title, String description) {
		OverlayItem newItem = new OverlayItem(title, description, p);
		overlayItemList.add(newItem);
		populate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		final int action = event.getAction();
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		boolean result = false;
		if (action == MotionEvent.ACTION_DOWN) {
			GeoPoint p1 = (GeoPoint) mapView.getProjection().fromPixels(x, y);
			Log.d("pp", String.valueOf(p1.getLatitudeE6()));
			for (OverlayItem item : overlayItemList) {
				Point p = new Point();
				double a=Math.abs(p1.getLatitudeE6()
						- item.getPoint().getLatitudeE6());
				double b= Math.abs(p1.getLongitudeE6()
						- item.getPoint().getLongitudeE6());
				if ( true) {
					result = true;
					inDrag = item;
					overlayItemList.remove(inDrag);
					populate();
					xDragTouchOffset = 0;
					yDragTouchOffset = 0;

					setDragImagePosition(p.x, p.y);
					dragImage.setVisibility(View.VISIBLE);

					xDragTouchOffset = x - p.x;
					yDragTouchOffset = y - p.y;
					break;
				}
			}
		} else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
			setDragImagePosition(x, y);
			result = true;
		} else if (action == MotionEvent.ACTION_UP && inDrag != null) {
			dragImage.setVisibility(View.GONE);

			GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels(
					x - xDragTouchOffset, y - yDragTouchOffset);
			OverlayItem toDrop = new OverlayItem(inDrag.getTitle(),
					inDrag.getSnippet(), pt);
			overlayItemList.add(toDrop);
			populate();
			inDrag = null;
			result = true;
		}

		return result || super.onTouchEvent(event, mapView);
	}

	@Override
	public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected OverlayItem createItem(int arg0) {
		// TODO Auto-generated method stub
		return overlayItemList.get(arg0);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlayItemList.size();
	}

	private void setDragImagePosition(int x, int y) {
		RelativeLayout.LayoutParams lp = (LayoutParams) dragImage
				.getLayoutParams();
		lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
				- yDragImageOffset - yDragTouchOffset, 0, 0);
		dragImage.setLayoutParams(lp);
	}

}
